import axios from 'axios'
import { Message } from 'element-ui'//引用elementui的信息提示框，根据个人选择可以不用
import Router from '../router'

// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
    return config
}, function (error) {
// Do something with request error
    Message.error({
        message: '请求错误：' + error.message
    })
    return Promise.reject(error)
})

// Add a response interceptor
axios.interceptors.response.use(function (response) {
// console.log('response')
// Do something with response data
// console.log(response)
//返回参数是noLogin时，返回首页
    if (response.data.flag === 'noLogin') {
        Router.push('/')
        Message.error({
            message: '当前会话过期'
        })
    }
    return response
}, function (error) {
// Do something with response error
    let msg = error.message
    if (error.response.status === 401) {
        msg = '当前会话已过期。'
        Router.push('/')
    }
    Message.error({
        message: '响应错误：' + msg
    })
    return Promise.reject(error)
})
export default axios